package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.HeightEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.HeightService;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebHeightContorller{

    @Resource
    private HeightService heightService;
    
    /**
     * 跳转身高到首页
     * @return
     */
    @RequestMapping(value = "/toHeightIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toHeightIndex() {
        ModelAndView mav = new ModelAndView("/sec/sec_shengao");
        
        return mav;
    }
    
    /**
     * 身高数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllHeight", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllHeight(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = heightService.getListHeightCount(name);
        
        //根据条件查询当前页数据
        List<HeightEntity> list = heightService.getAllHeight(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<HeightEntity> page = new Page<HeightEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<HeightEntity> pageModel = new PageModelList<HeightEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 身高添加
     */
    @RequestMapping(value = "/toSaveHeight")
    @ResponseBody
    public int toSaveHeight(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        
        HeightEntity heightEntity = new HeightEntity();
        
        heightEntity.setName(name);
        heightEntity.setPrice(price);
        
        int flag = heightService.saveHeight(heightEntity);
        
        return flag;
    }
    
    /**
     * 删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteHeightId")
    @ResponseBody
    public int toDeleteHeightId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = heightService.deleteHeightId(id);
        return flag;
    }
    
    /**
     * 身高修改
     */
    
    @RequestMapping(value = "/toUpdateHeightId")
    @ResponseBody
    public int toUpdateHeightId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        
        HeightEntity heightEntity = new HeightEntity();
        heightEntity.setName(name);
        heightEntity.setPrice(price);
        heightEntity.setId(id);
        
        int flag = heightService.updateHeightId(heightEntity);
        return flag;
    }
    
}
